package com.salinity.kun.helper;

import com.salinity.kun.algorithm.FourierSeries;
import com.salinity.kun.model.FourierSeriesInput;

public class FourierSeriesHelper {

	private FourierSeriesInput fourierSeriesInput;
	private FourierSeries fourierSeries;
	private double[] ftCalData;

	public FourierSeriesHelper(FourierSeriesInput fourierSeriesInput) {
		this.fourierSeriesInput = fourierSeriesInput;
		init();
	}

	private void init() {
		double deltaT = fourierSeriesInput.getDeltaT();
		Double[] ft = fourierSeriesInput.getF();
		int seriesCount = fourierSeriesInput.getSeriesCount();
		double t0 = fourierSeriesInput.getT();
		if (seriesCount <= 0.0) {
			fourierSeries = new FourierSeries(deltaT, ft, t0);
		} else {
			fourierSeries = new FourierSeries(deltaT, ft, seriesCount, t0);
		}
	}

	public FourierSeriesInput getFourierSeriesInput() {
		return fourierSeriesInput;
	}

	public double get_a(int index) {
		return fourierSeries.get_a(index);
	}

	public double[] get_a() {
		return fourierSeries.get_a();
	}

	public double get_b(int index) {
		return fourierSeries.get_b(index);
	}

	public double[] get_b() {
		return fourierSeries.get_b();
	}

	public double get_sqrt_a_b(double a, double b) {
		return Math.sqrt(a * a + b * b);
	}

	public double[] get_sqrt_a_b(double[] a, double[] b) {
		if (a == null || b == null) {
			return null;
		}
		double[] rslt = new double[b.length];

		for (int i = 1; i < b.length; i++) {
			rslt[i] = get_sqrt_a_b(a[i], b[i]);
		}
		return rslt;
	}

	public double get_theta_i(double a, double b) {
		return Math.asin(-b / get_sqrt_a_b(a, b));
	}

	public double getInitialPhase(double a, double b) {
		return Math.atan(-b / a);
	}

	public double get_fs(int index) {
		return fourierSeries.getFs(index);
	}

	/**
	 * 获取deltaT整数倍位置的函数值
	 * @param index
	 * @return
	 */
	public double getFtCalByIndex(int index) {
		return fourierSeries.getFtCalByIndex(index);
	}

	/**
	 * 获取指定时间位置的函数值，时间范围为[-T/2,T/2]
	 * @param t
	 * @return
	 */
	
	public double getFtCalByTime(double t) {
		return fourierSeries.getFtCalByTime(t);
	}
	
	public double[] getFtCalByIndex() {
		if (ftCalData != null)
			return ftCalData;
		ftCalData = new double[fourierSeries.getFtLength()];
		for (int i = 0; i < ftCalData.length; i++) {
			ftCalData[i] = getFtCalByIndex(i);
		}
		return ftCalData;
	}

}
